package com.qf.hello;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @Description:
 * @Author: xbb
 * @Date:2020/4/20
 */
public class ServerDemo {
    public static void main(String[] args) {
        try {
            //1.创建两个线程池(主，从)
            EventLoopGroup master = new NioEventLoopGroup();    //主线程主要负责连接
            EventLoopGroup slaver = new NioEventLoopGroup();    //从线程主要负责读写

            //2.创建引导类
            ServerBootstrap serverBootstrap = new ServerBootstrap();
//            serverBootstrap.group();    //单线程
//            serverBootstrap.group(master);  //多线程
            serverBootstrap.group(master,slaver);   //主从线程模型
            serverBootstrap.channel(NioServerSocketChannel.class);  //设置通道的类型
            serverBootstrap.childHandler(new ServerSocketHandler()); //处理客户端的handler

            //绑定端口号是异步完成的
            ChannelFuture bind = serverBootstrap.bind(8080);    //设置服务端端口号
            bind.sync();    //阻塞住，当绑定端口完成之后释放
            System.out.println("服务端启动完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
